package transaction

import (
	"bytes"
	"fmt"
)

func GetEndorserNameByBytes(byt []byte) (*string, error) {
	start_index := bytes.Index(byt, []byte("-----BEGIN"))
	if start_index == -1 {
		return nil, fmt.Errorf("No certificate found")
	}

	bytTmp := byt[:start_index]
	start_index = bytes.Index(bytTmp, []byte{0x0a})
	if start_index == -1 {
		return nil, fmt.Errorf("start flag not found")
	}
	start_index += 2

	// end
	bytTmp = bytTmp[start_index:]
	end_index := bytes.Index(bytTmp, []byte{0x12})
	if end_index == -1 {
		return nil, fmt.Errorf("end flag not found")
	}

	sTmp := bytTmp[:end_index]
	s := string(sTmp)
	return &s, nil
}
